/**
 * Created by ZhiyangLee on 2016/8/12.
 */

KISSY.use('node,pic/friendship/1.0.0/', function(s, node, friendship) {
    if(node.all('[pic_object_name="change_friendship"]').length == 0) return ;
    node.all('[pic_object_name="change_friendship"]').on('click', function(e) {

        var target = s.one(e.currentTarget);
        var action = target.attr('pic_action');
        var followUid = target.attr('pic_action_value');

        if (action == 'create_friendship') {

            /**
             * 关注一个摄影师
             */
            if (CONFIG.isLogin == 0) {

                env.login();

                return false;

            }

            if (followUid) {

                friendship.createFriendship(followUid);

                target.attr('pic_action', 'destroy_friendship');

                if (target.attr('pic_action_replace_text')) {

                    var replaceText = target.attr('pic_action_replace_text');
                    var oldText = target.html();

                    target.html(replaceText);

                    target.attr('pic_action_replace_text', oldText)

                } else {

                    target.html('取消关注');

                }


            } else {

                alert('页面数据异常');

            }

        } else if (action == 'destroy_friendship') {

            /**
             * 取消关注
             */
            if (CONFIG.isLogin == 0) {

                alert('异常');
                return false;

            }

            if (followUid) {

                var followItems = s.all('[pic_object_item="' + followUid + '"]');

                if (followItems) {

                    var isRemove = true;

                    if (target.attr('pic_action_is_remove')) {

                        isRemove = target.attr('pic_action_is_remove') == "true";

                    }

                    if (isRemove) {

                        followItems.remove();

                    }

                }

                friendship.destroyFriendship(followUid);

                target.attr('pic_action', 'create_friendship');

                if (target.attr('pic_action_replace_text')) {

                    var replaceText = target.attr('pic_action_replace_text');
                    var oldText = target.html();

                    target.html(replaceText);

                    target.attr('pic_action_replace_text', oldText)

                } else {

                    target.html('加关注');

                }

            } else {

                alert('页面数据异常!');

            }

        } else {

            //....

        }

    });

    if(node.one('#batch-follow')) {
        node.one('#batch-follow').on('click', function(e) {

            var batchBtn = node.one(e.currentTarget);
            var batchBtnState = batchBtn.attr('pic_object_state');

            //正在处理中则返回
            if (batchBtnState == 'batch-following' || batchBtnState == 'batch-follow-success') {

                e.stopPropagation();
                return false;

            }

            var followElementList = node.all('[pic_action="create_friendship"]');

            if (followElementList) {

                batchBtn.html('正在关注...');
                batchBtn.attr('pic_object_state', 'batch-following');
                batchBtn.css('background-color', '#5B5C5D');
                followElementList.hide();


                var followElementListLastIndex = followElementList.length - 1;

                //遍历并且批量关注
                followElementList.each(function(item, index) {


                    var target = s.one(item);
                    var followUid = target.attr('pic_action_value');

                    if (index == followElementListLastIndex) {

                        friendship.createFriendship(followUid, {

                            success : function(rep) {

                                batchBtn.html('关注成功!准备跳转...');
                                batchBtn.removeAttr('pic_object_state');
                                batchBtn.removeClass('background-color');

                                window.location.reload();

                            }

                        });

                    } else {

                        friendship.createFriendship(followUid);

                    }

                    target.attr('pic_action', 'destroy_friendship');
                    target.html('取消关注');

                });

            }

        });
    }

});